import axios from 'axios';
import config from './config';

const instance = axios.create({
    baseURL: config.baseUrl,
    headers: config.headers,
    timeout: config.timeout,
    withCredentials: config.withCredentials
})

// 请求拦截器
instance.interceptors.request.use(
    config => {

        return config;
    },
    error => {
        return Promise.reject(error);
    }
)

// 返回拦截器
instance.interceptors.response.use(
    response => {
        let data;
        // IE9时response.data是undefined，因此需要使用response.request.responseText(Stringify后的字符串)
        if (response.data == undefined) {
          data = JSON.parse(response.request.responseText)
        } else {
          data = response.data
        }

        return data;
    },
    err => {
        return Promise.reject(err);
    }
)

export default instance;